﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Common;
using HanGu.Entity;
using HanGu.Services.Interface;

namespace Hangu.WebClient.Controllers.Admin
{
    public class AdminBaseController : Controller
    {
        protected static readonly IMessage serviceMessage = ServiceFactory.GetService<IMessage>();
        protected static readonly IFileManage serviceFileManage = ServiceFactory.GetService<IFileManage>();

        protected static readonly int pageSize = Helper.PageSize;
        protected string sort = "";
        protected string order = "";
        protected int pageNumber = 1;

        protected const string VIEWLIST = "~/Views/Admin/{0}/index.cshtml";
        protected const string VIEWCREATE = "~/Views/Admin/{0}/create.cshtml";
        /// <summary>
        /// 列表视图
        /// </summary>
        protected string viewList = string.Empty;
        /// <summary>
        /// 创建或修改视图
        /// </summary>
        protected string viewCreate = string.Empty;

        protected string ajaxStr = "\"statusCode\":\"{0}\", \"message\":\"{1}\", \"navTabId\":\"{2}\", \"forwardUrl\":\"{3}\", \"callbackType\":\"{4}\"";
        private UniversallySearchParam _SearchParma = new UniversallySearchParam() { Pagination = new PaginationDTO() { CurrentIndex = 1, PageSize = pageSize } };
        protected UniversallySearchParam SearchParma
        {
            get { return _SearchParma; }
            set { _SearchParma = value; }
        }

        /// <summary>
        /// navTab ID
        /// </summary>
        protected string navTabId = string.Empty;

        private CurrentUserDTO _CurentUser = new CurrentUserDTO() { UserName = "admin" };
        public CurrentUserDTO CurentUser
        {
            get
            {
                return _CurentUser;
            }
            set { _CurentUser = value; }
        }

        [ValidateInput(false)]
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Session["CurentUser"] != null)
            {
                CurentUser = Session["CurentUser"] as CurrentUserDTO;
            }
            else
            {                
                Response.Redirect("admin/login");
            }

            ViewData["pageSize"] = pageSize;
            ViewData["PhotoWidth"] = Helper.PhotoWidth;
            ViewData["PhotoHeight"] = Helper.PhotoHeight;
            ViewData["SearchParma"] = SearchParma;
            #region 设置当前页面
            int currentIndex = 1;
            if (Request["pageNum"] != null)
            {
                int.TryParse(Request["pageNum"], out currentIndex);
            }
            SearchParma.Pagination.CurrentIndex = currentIndex;
            #endregion

            #region 设置共用搜索参数 标题  开始结束时间
            SearchParma.Ttile = Request["Title"];
            DateTime startime = DateTime.MinValue;
            DateTime endtime = DateTime.MinValue;
            if (Request["StarTime"] != null)
            {
                if (DateTime.TryParse(Request["StarTime"], out startime))
                {
                    { SearchParma.StarDateTime = startime; }
                }
            }
            if (Request["EndTime"] != null)
            {
                if (DateTime.TryParse(Request["EndTime"], out endtime))
                { SearchParma.EndDateTime = endtime; }
            }
            #endregion

            #region 设置是否需要重新计算总记录
            if (Request["RowCount"] != null)
            {
                int rowCount = 0;
                int.TryParse(Request["RowCount"], out rowCount);
                SearchParma.Pagination.RowCount = rowCount;
            }
            #endregion
            ViewData["CurrentController"] = navTabId = filterContext.Controller.GetType().Name.Replace("Controller", "");

            #region 初始化视图路径
            viewList = string.Format(VIEWLIST, navTabId);
            viewCreate = string.Format(VIEWCREATE, navTabId);
            #endregion


        }

        protected void ExceptionHand(Exception e)
        {
            new Loger().ExceptionHand(e);
        }

        protected override void OnException(ExceptionContext errorContext)
        {
            errorContext.Result = new RedirectResult(Url.Action("Error", "Shared"));
        }

        protected virtual bool MoveToFile(string guid, string newFile,out string newpath)
        {
            bool tag = false;
            newpath = string.Empty;
            try
            {
                var fileManage = serviceFileManage.GetByGuid(guid);
                //string newFile = "Email";
                foreach (var item in fileManage)
                {
                    if (item.FilePath.IndexOf("Temp") > 0)
                    {
                        FileManagement.MoveToFile(item.FilePath, item.FilePath.Replace("Temp", newFile));
                        item.FilePath = item.FilePath.Replace("Temp", newFile);
                        newpath = item.FilePath;
                        serviceFileManage.Update(item);
                    }
                }
                tag = true;
            }
            catch
            { }
            return tag;

        }
    }
}
